﻿@*@model CallCenter.Web.Models.LoginViewModel

@{
    ViewBag.Title = "Call Center Log in";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@{ 
Page.Title = "Call Center Log in";

// Initialize general page variables
var email = "";
var password = "";
var rememberMe = false;

//// Setup validation
Validation.RequireField("email", "You must specify an email address.");
Validation.RequireField("password", "You must specify a password.");

// If this is a POST request, validate and process data
if (IsPost)
{
    // is this a OAuth sign in request?
    string provider = Request.Form["provider"];
    if (!provider.IsEmpty())
    {
        OAuthWebSecurity.RequestAuthentication(provider, "~/Account/RegisterService");
        return;
    }
    else if (Validation.IsValid())
    {
        email = Request.Form["email"];
        password = Request.Form["password"];
        rememberMe = Request.Form["rememberMe"].AsBool();
        if (email == "dan" && password == "resnav")
        {
            Response.Redirect("~/callCenter_Web/ScratchPadNew.aspx");
        }

        else if (email == "employee" && password == "resnavemployee")
        {
            Session["employeeAuth"] = true;
            Response.Redirect("~/Account/Register");
        }

        if (WebSecurity.UserExists(email) && WebSecurity.GetPasswordFailuresSinceLastSuccess(email) > 4 &&
            WebSecurity.GetLastPasswordFailureDate(email).AddSeconds(60) > DateTime.UtcNow)
        {
            Response.Redirect("~/Account/AccountLockedOut");
            return;
        }

        // Attempt to log in to the Security object using provided creds
        if (WebSecurity.Login(email, password, rememberMe))
        {
            var returnUrl = Request.QueryString["ReturnUrl"];
            if (returnUrl.IsEmpty())
            {
                Session.Timeout = 5000;
                Session["currentUserId"] = WebSecurity.CurrentUserId;
                Session["currentUserName"] = WebSecurity.CurrentUserName;
                var userRole = Roles.GetRolesForUser(WebSecurity.CurrentUserName);
                //Session["currentUserRole"] = userRole[0];
                //Session["currentUserRole"] = WebSecurity.

                Session["loggedIn"] = true;

                Response.Redirect("~/callCenter_Web/ScratchPadNew.aspx");

            }
            else
            {
                Context.RedirectLocal(returnUrl);
            }
        }
        else
        {
            ModelState.AddFormError("The user name or password provided is incorrect.");
        }
    }
}
}

    <div class="row">
        <div class="col-md-8">
            <section id="loginForm">
                <form method="post">
                @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                    @Html.AntiForgeryToken()
                    <h4>Use a local account to log in.</h4>
                    <hr />
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                        <div class="col-md-10">
                            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                        <div class="col-md-10">
                            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <div class="checkbox">
                                @Html.CheckBoxFor(m => m.RememberMe)
                                @Html.LabelFor(m => m.RememberMe)
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Log in" class="btn btn-default" />
                        </div>
                    </div>
                    <p>
                        @Html.ActionLink("Register as a new user", "Register")
                    </p>
                    @* Enable this once you have account confirmation enabled for password reset functionality
                        <p>
                            @Html.ActionLink("Forgot your password?", "ForgotPassword")
                        </p>*@
                @*}
                    </form>
            </section>
        </div>
        </div>

      @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    } 

</body>
</html>*@

